package com.qf.flash.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.model.MyLaunchBean;
import com.qf.flash.model.TeamMateBean;
import com.qf.flash.mvp.contract.GroupListContract;
import com.qf.flash.mvp.presenter.GroupListPresenter;
import com.qf.flash.ui.home.adapter.MyTeamItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyTeamsActivity extends BaseActivity implements GroupListContract.View, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.rv_frame)
    RecyclerView mRecyclerView;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;

    private RVAdapter<Object> mAdapter;
    private GroupListPresenter groupListPresenter;
    private int page = 1, size = 30;
    boolean isfresh, isLoadMore;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_balance_money;
    }

    @Override
    protected void initInject() {
        super.initInject();
        groupListPresenter = new GroupListPresenter();
        initPresenters(groupListPresenter);
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("我的团队");

        //   -------------------
        mAdapter = new RVAdapter<>(new MyTeamItem());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);

        mRefresh.autoRefresh();
    }


    public static void start(Activity activity) {
        Intent intent = new Intent(activity, MyTeamsActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_top_back, R.id.tv_top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;

        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isfresh = true;
        page = 1;
        groupListPresenter.getGroupList(page, size);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isLoadMore = true;
        page++;
        groupListPresenter.getGroupList(page, size);
    }

    @Override
    public void setMyMate(List<TeamMateBean> teamMateBeans, int totalPage) {
        mRefresh.setEnableLoadMore(page < totalPage);

        List<Object> datas = mAdapter.getDatas();
        if (isfresh) {
            mRefresh.finishRefresh();
            datas.clear();
            datas.addAll(teamMateBeans);
        } else if (isLoadMore) {
            mRefresh.finishLoadMore();
            datas.addAll(teamMateBeans);
        }
        mAdapter.notifyDataSetChanged();

        isfresh = false;
        isLoadMore = false;
    }
}
